<template>
  <div class="p-2">
    <transition :enter-active-class="searchAnimate.enter" :leave-active-class="searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">

          <el-form-item :label="t('view.i18nConfig-language')" prop="language">
              <el-select v-model="queryParams.language" :placeholder="t('common.inputText')" filterable  clearable style="width: 240px">
                  <el-option v-for="dict in sys_language" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
          </el-form-item>
            <el-form-item :label="t('view.i18nConfig-type')" prop="type">
                <el-select v-model="queryParams.type" :placeholder="t('common.inputText')" filterable  clearable style="width: 240px">
                    <el-option v-for="dict in sys_i18n_type" :key="dict.value" :label="dict.label" :value="dict.value" />
                </el-select>
            </el-form-item>
          <el-form-item :label="t('view.i18nConfig-enCode')" prop="enCode">
            <el-input v-model="queryParams.enCode" :placeholder="t('common.inputText')" clearable @keyup.enter="handleQuery" />
          </el-form-item>
                <el-form-item :label="t('view.i18nConfig-content')" prop="content">
                      <el-input v-model="queryParams.content" :placeholder="t('common.inputText')" clearable @keyup.enter="handleQuery" />
                    </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">{{t('common.searchText')}}</el-button>
            <el-button icon="Refresh" @click="resetQuery">{{t('common.resetText')}}</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['system:i18nConfig:add']">{{t('common.addText')}}</el-button>
          </el-col>
          <el-col :span="1.5">
              <el-button type="primary" plain icon="Plus" :disabled="single" @click="handleCopy()" v-hasPermi="['system:i18nConfig:add']">{{t('common.copyText')}}</el-button>
           </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['system:i18nConfig:edit']">{{t('common.editText')}}</el-button>
          </el-col>

          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['system:i18nConfig:remove']">{{t('common.delText')}}</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['system:i18nConfig:export']">{{t('common.exportText')}}</el-button>
          </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="Upload" @click="handleImport" v-hasPermi="['system:i18nConfig:add']">{{t('common.importText')}}</el-button>
            </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

        <el-table v-loading="loading" :data="i18nConfigList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column :label="t('view.i18nConfig-id')"  align="center" prop="id" v-if="true" />
            <el-table-column :label="t('view.i18nConfig-type')"  align="center" prop="type" >
                <template #default="scope">
                    <dict-tag :options="sys_i18n_type" :value="scope.row.type" />
                </template>
            </el-table-column>
            <el-table-column :label="t('view.i18nConfig-language')"  align="center" prop="language" >
                <template #default="scope">
                    <dict-tag :options="sys_language" :value="scope.row.language" />
                </template>
            </el-table-column>
            <el-table-column :label="t('view.i18nConfig-enCode')"  align="center" prop="enCode" />
            <el-table-column :label="t('view.i18nConfig-content')"  align="center" prop="content" />
            <el-table-column :label="t('view.i18nConfig-seq')"  align="center" prop="seq" />
            <el-table-column :label="t('common.operatingText')" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-tooltip :content="t('common.copyText')" placement="top">
                        <el-button link type="primary" icon="Plus" @click="handleCopy(scope.row)" v-hasPermi="['system:i18nConfig:add']"></el-button>
                    </el-tooltip>
                    <el-tooltip :content="t('common.editText')" placement="top">
                        <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:i18nConfig:edit']"></el-button>
                    </el-tooltip>
                    <el-tooltip :content="t('common.delText')" placement="top">
                        <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:i18nConfig:remove']"></el-button>
                    </el-tooltip>
                </template>
            </el-table-column>
        </el-table>


      <pagination
          v-show="total>0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改国际化配置对话框 -->
      <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
          <el-form ref="i18nConfigFormRef" :model="form" :rules="rules" label-width="80px">
              <el-form-item :label="t('view.i18nConfig-content')">
                  <editor v-model="form.content" :min-height="192"/>
              </el-form-item>
              <el-form-item :label="t('view.i18nConfig-seq')" prop="seq">
                  <el-input v-model="form.seq" :placeholder="t('common.inputText')" />
              </el-form-item>
          </el-form>
          <template #footer>
              <div class="dialog-footer">
                  <el-button :loading="buttonLoading" type="primary" @click="submitForm">{{t('common.okText')}}</el-button>
                  <el-button @click="cancel">{{t('common.cancelText')}}</el-button>
              </div>
          </template>
      </el-dialog>

      <!-- 导入 -->
      <ImportForm ref="importFormRef" url="/system/i18nConfig/import" :importTemplateData="importTemplateData"
                  @success="importSuccess"/>
  </div>
</template>

<script setup name="I18nConfig" lang="ts">
import { listI18nConfig, getI18nConfig, delI18nConfig, addI18nConfig, updateI18nConfig } from '@/api/system/i18nConfig';
import { I18nConfigVO, I18nConfigQuery, I18nConfigForm } from '@/api/system/i18nConfig/types';
import { ComponentInternalInstance } from 'vue';
import { ElForm } from 'element-plus';
import {searchAnimate} from "@/animate";
import {useI18n} from '@/hooks/web/useI18n';
const { t } = useI18n();
const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const i18nConfigList = ref<I18nConfigVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref(ElForm);
const i18nConfigFormRef = ref(ElForm);

const { sys_i18n_type } = toRefs<any>(proxy?.useDict("sys_i18n_type"))
const { sys_language } = toRefs<any>(proxy?.useDict("sys_language"))

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: I18nConfigForm = {
  id: undefined,
  type: undefined,
  language: undefined,
  enCode: undefined,
  content: undefined,
  seq: undefined,
}
const data = reactive<PageData<I18nConfigForm, I18nConfigQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    type: undefined,
    language: undefined,
    enCode: undefined,
    content: undefined,
    seq: undefined,
  },
  rules: {
    id: [
      { required: true, message: "自然主键不能为空", trigger: "blur" }
    ],
    type: [
      { required: true, message: "类型不能为空", trigger: "change" }
    ],
    language: [
      { required: true, message: "语言不能为空", trigger: "change" }
    ],
    enCode: [
      { required: true, message: "编号不能为空", trigger: "blur" }
    ],
    content: [
      { required: true, message: "值不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询国际化配置列表 */
const getList = async () => {
  loading.value = true;
  const res = await listI18nConfig(queryParams.value);
  i18nConfigList.value = res.rows;
  total.value =parseInt(res.total);
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  i18nConfigFormRef.value.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: I18nConfigVO[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  dialog.visible = true;
  dialog.title = "添加国际化配置";
  nextTick(() => {
    reset();
  });
}

/** 修改按钮操作 */
const handleUpdate = (row?: I18nConfigVO) => {
  loading.value = true
  dialog.visible = true;
  dialog.title = "修改国际化配置";
  nextTick(async () => {
    reset();
    const _id = row?.id || ids.value[0]
    const res = await getI18nConfig(_id);

    loading.value = false;
    Object.assign(form.value, res.data);
  });
}

/** 复制按钮操作 */
const handleCopy = (row?: I18nConfigVO) => {
  loading.value = true
  dialog.visible = true;
  dialog.title = "复制国际化配置";
  nextTick(async () => {
    reset();
    const _id = row?.id || ids.value[0]
    const res = await getI18nConfig(_id);
    res.data.id=null;
    loading.value = false;
    Object.assign(form.value, res.data);
  });
}

/** 提交按钮 */
const submitForm = () => {
  i18nConfigFormRef.value.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateI18nConfig(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addI18nConfig(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("修改成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: I18nConfigVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除国际化配置编号为"' + _ids + '"的数据项？').finally(() => loading.value = false);
  await delI18nConfig(_ids);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('system/i18nConfig/export', {
    ...queryParams.value
  }, `i18nConfig_${new Date().getTime()}.xlsx`)
}

/** 导入 */
const importFormRef = ref()
const handleImport = () => {
    importFormRef.value.open()
}
// 导入附件弹窗所需的参数
const importTemplateData = reactive({
    templateUrl: '/system/i18nConfig/getImportTemplate',
    templateTitle: `i18nConfig_${new Date().getTime()}.xlsx`
})
// 导入成功之后
const importSuccess = () => {
    getList()
}

onMounted(() => {
  getList();
});
</script>
